package com.hrc.springbootdemo.async;

import jakarta.annotation.PostConstruct;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;


import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@Service
public class FutureTaskService {

    private ThreadPoolTaskExecutor executor;


    @PostConstruct
    public void init() {
        executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("FutureTaskService-");
        executor.initialize();
    }
    public void test() throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(() -> Thread.currentThread().getName());
        executor.submit(futureTask);

            System.out.println("futureTask.get() = " + futureTask.get());
            executor.execute(() -> System.out.println(Thread.currentThread().getName()));
        System.out.println("executor.getCorePoolSize() = " + executor.getCorePoolSize());
        executor.submit(() -> System.out.println(Thread.currentThread().getName()));
    }
}
